﻿using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.StorageClient;
using Azure.Toolkit.Azure;
using AzureFacebook.Domain;

namespace AzureFacebook.Web
{
    public class WebRole : AzureRoleEntryPoint
    {
        protected override void Configure(DiagnosticMonitorConfiguration config)
        {
            base.Configure(config);
            //Performance Counter for cpu usage
            var cpuUsage = new PerformanceCounterConfiguration
            {
                CounterSpecifier = @"\Processor(_Total)\% Processor Time",
                SampleRate = TimeSpan.FromSeconds(5)
            };

            //Performance Counter Subscription for Memory
            var pccMemory = new PerformanceCounterConfiguration
            {
                CounterSpecifier = @"\Memory\Available Mbytes",
                SampleRate = TimeSpan.FromSeconds(5)
            };

            // ASP.NET Applications(__Total__)\Requests/Sec
            var requestsPerSec = new PerformanceCounterConfiguration
            {
                CounterSpecifier = @"\ASP.NET Applications(__Total__)\Requests/Sec",
                SampleRate = TimeSpan.FromSeconds(5)
            };

            config.PerformanceCounters.DataSources.Add(cpuUsage);
            config.PerformanceCounters.DataSources.Add(pccMemory);
            config.PerformanceCounters.DataSources.Add(requestsPerSec);
        }

        static void CreateTables()
        {
            var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            CloudTableClient client = storageAccount.CreateCloudTableClient();

            // i put it into the tablerepositroy
            //client.CreateTableIfNotExist(TableNames.CommentTableName);
        }

        public override bool OnStart()
        {
            bool started = base.OnStart();
            CreateTables();
            return started;
        }
    }
}